package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.CourseService;
import com.yuwubao.uav.tool.util.ArticleSearchService;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 *课题管理
 */
@RestController
@RequestMapping("/bk")
public class CourseController {

    private static final Logger logger = LogManager.getLogger(CourseController.class);

    @Resource
    private CourseService courseService;
    @Resource
    private ArticleSearchService articleSearchService;
    @LoginRequired
    @PostMapping("/addCourse")
    @ApiOperation(value = "新增课题")
    public JsonResult addCourse(
            @ApiParam(value = "课题名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt,
            @ApiParam(value = "发布机构") @RequestParam(value = "publicOrg",required = false) String publicOrg,
            @ApiParam(value = "所属区域") @RequestParam(value = "belongArea",required = false) String belongArea,
            @ApiParam(value = "合作类型") @RequestParam(value = "corpType",required = false) String corpType,
            @ApiParam(value = "联系人") @RequestParam(value = "contactName",required = false) String contactName,
            @ApiParam(value = "课题类型ID") @RequestParam(value = "course_type_id",required = false) String courseTypeId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("name", name);
            pageData.put("content", content);
            pageData.put("publicAt", publicAt);
            pageData.put("publicOrg", publicOrg);
            pageData.put("belongArea", belongArea);
            pageData.put("corpType", corpType);
            pageData.put("contactName", contactName);
            pageData.put("courseTypeId", courseTypeId);

            courseService.addCourse(pageData);

            try {
                pageData.put("tableName","t_course");
                articleSearchService.createDoc(pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateCourse")
    @ApiOperation(value = "更新课题")
    public JsonResult updateCourse(
            @ApiParam(value = "课题ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "课题名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt,
            @ApiParam(value = "发布机构") @RequestParam(value = "publicOrg",required = false) String publicOrg,
            @ApiParam(value = "所属区域") @RequestParam(value = "belongArea",required = false) String belongArea,
            @ApiParam(value = "合作类型") @RequestParam(value = "corpType",required = false) String corpType,
            @ApiParam(value = "联系人") @RequestParam(value = "contactName",required = false) String contactName,
            @ApiParam(value = "课题类型ID") @RequestParam(value = "course_type_id",required = false) String courseTypeId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("content", content);
            pageData.put("publicAt", publicAt);
            pageData.put("publicOrg", publicOrg);
            pageData.put("belongArea", belongArea);
            pageData.put("corpType", corpType);
            pageData.put("contactName", contactName);
            pageData.put("courseTypeId", courseTypeId);

            courseService.updateCourse(pageData);

            try {
                pageData.put("tableName","t_course");
                articleSearchService.update(id,pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteCourse")
    @ApiOperation(value = "删除课题")
    public JsonResult deleteCourse(
            @ApiParam(value = "课题ID,多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            courseService.deleteCourse(pageData);

            try {
                if (StringUtil.isNotNvlOrBlank(ids)) {
                    pageData.put("tableName","t_course");
                    for (String id : ids.split(",")) {
                        pageData.put("id",id);
                        articleSearchService.delete(pageData);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listCourse")
    @ApiOperation(value = "获取课题列表")
    public JsonResult listCourse(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(courseService.listCourse(pageData));
            result.setResultNum(courseService.listCourseCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


    @LoginRequired
    @PostMapping("/addCourseType")
    @ApiOperation(value = "新增课题类型")
    public JsonResult addCourseType(
            @ApiParam(value = "课题类型,多个用逗号分隔", required = true) @RequestParam("names") String names
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);

            courseService.addCourseType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateCourseType")
    @ApiOperation(value = "更新课题类型")
    public JsonResult updateCourseType(
            @ApiParam(value = "课题类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "课题类型名称", required = true) @RequestParam("name") String name
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);

            courseService.updateCourseType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteCourseType")
    @ApiOperation(value = "删除课题类型")
    public JsonResult deleteCourseType(
            @ApiParam(value = "课题类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            courseService.deleteCourseType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listCourseType")
    @ApiOperation(value = "获取课题类型列表")
    public JsonResult listCourseType(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(courseService.listCourseType(pageData));
            result.setResultNum(courseService.listCourseTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


}
